<template>
  <div ref="wrapper" class="wrapper" :class="{ init: isInit }">
    <ul class="content">
      <li
        ref="item"
        @click="() => handleClick(index)"
        v-for="(item, index) in arrList"
        :key="item.num"
        class="item"
        :class="{ active: item.isActive }"
      >
        <div class="inner">
          <div class="bg"></div>
          <div class="title">
            <h2>{{item.name}}</h2>
          </div>
          <div class="desc">
            <div @click.stop="close(index)" class="close"></div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Pic",
  data() {
    return {
      isInit: true,
      arrList: [
        { num: 1, isActive: false, name: '灭菌消毒机器人' },
        { num: 2, isActive: false, name: '物流机器人' },
        { num: 3, isActive: false, name: '警用机器人' },
        { num: 4, isActive: false, name: '配送机器人' },
        { num: 5, isActive: false, name: '供给机器人' },
        { num: 6, isActive: false, name: '巡逻机器人' },
      ],
    };
  },
  methods: {
    handleClick(i) {
      this.arrList[i].isActive = true;
      this.$refs.wrapper.classList.add("w-active");
    },
    close(i) {
    this.arrList[i].isActive = false;
      this.$refs.wrapper.classList.remove("w-active");
    },
  },
  mounted() {
    setTimeout(() => {
      this.isInit = false;
    }, 1000);
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #313234;
  color: #fff;
}

.wrapper .content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 80%;
  height: 80%;
}

.wrapper .content .item {
  width: 16%;
  height: 100%;
  border-radius: 20px;
  overflow: hidden;
  background: #333;
  cursor: pointer;
  transition: height 0.5s linear 0.5s, width 0.5s linear;
}

.wrapper .content .item .inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.5s linear;
  border-radius: 20px;
  overflow: hidden;
}
/* translate3d开启GPU加速 类名控制 */
.init .content .item .inner {
  transform: translate3d(0, 100%, 0);
}

.wrapper .content .active {
  width: 100%;
}

.w-active .content .item {
  transition: height 0.5s linear, width 0.5s linear 0.5s;
}

.w-active .content .item:not(.active) {
  width: 0%;
  height: 0%;
}

.w-active .content .item .inner .title {
  opacity: 0;
}

.wrapper .content .item:nth-of-type(2) .inner {
  transition-delay: 0.1s;
}
.wrapper .content .item:nth-of-type(3) .inner {
  transition-delay: 0.2s;
}
.wrapper .content .item:nth-of-type(4) .inner {
  transition-delay: 0.3s;
}
.wrapper .content .item:nth-of-type(5) .inner {
  transition-delay: 0.4s;
}
.wrapper .content .item:nth-of-type(6) .inner {
  transition-delay: 0.5s;
}

.wrapper .content .item .bg {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  opacity: 0.3;
}

.wrapper .content .item:nth-of-type(1) .inner .bg {
  background-image: url("../assets/image/img/1.jpg");
}
.wrapper .content .item:nth-of-type(2) .inner .bg {
  background-image: url("../assets/image/img/2.jpg");
}
.wrapper .content .item:nth-of-type(3) .inner .bg {
  background-image: url("../assets/image/img/3.jpg");
}
.wrapper .content .item:nth-of-type(4) .inner .bg {
  background-image: url("../assets/image/img/4.jpg");
}
.wrapper .content .item:nth-of-type(5) .inner .bg {
  background-image: url("../assets/image/img/5.jpg");
}
.wrapper .content .item:nth-of-type(6) .inner .bg {
  background-image: url("../assets/image/img/6.jpg");
}

.wrapper .content .item .inner .title {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  transition: opacity 0.3s linear;
  h2 {
      width: 12rem;
  }
}

.wrapper .content .item .inner .title h2 {
  font-size: 14px;
}

.wrapper .content .item:hover .title h2 {
  font-size: 16px;
}

.wrapper .content .item:hover .inner .bg {
  opacity: 1;
}

.wrapper .content .item .inner .desc {
  position: absolute;
  top: 30px;
  width: 100%;
  height: 30px;
  opacity: 0;
  transition: opacity 0.5s linear;
}

.wrapper .content .active .inner .desc {
  opacity: 1;
  transition: opacity 0.5s linear 1s;
}

.wrapper .content .item .inner .desc .left {
  float: left;
  margin-left: 30px;
  line-height: 30px;
}

.wrapper .content .item .inner .desc .close {
  position: relative;
  float: right;
  width: 30px;
  height: 30px;
  margin-right: 30px;
}

.wrapper .content .item .inner .desc .close::before,
.wrapper .content .item .inner .desc .close::after {
  /* before和after必须要有content */
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  width: 30px;
  height: 2px;
  margin-top: -1px;
  background: #fff;
}

.wrapper .content .item .inner .desc .close::before {
  transform: rotate(-45deg);
}

.wrapper .content .item .inner .desc .close::after {
  transform: rotate(45deg);
}
</style>